package com.lzb.newloan;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jungly.gridpasswordview.GridPasswordView;
import com.lzb.newloan.utils.BaseActivity;
import com.lzb.newloan.utils.Config;
import com.lzb.newloan.utils.EditUtils;
import com.lzb.newloan.utils.HttpUtils;
import com.lzb.newloan.utils.TimeCount;

import org.json.JSONException;
import org.json.JSONObject;

public class PayPwdActivity extends BaseActivity implements OnClickListener {

	private EditText phone,code;
	private Button comfirm_pay_btn;
	private String userId ;
	private Button codeBtn;
	private TimeCount timeC;
	private GridPasswordView  gridPasswordView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_update_pay_pwd);
		initView();
	}

	private void initView() {
		SharedPreferences sp = getSharedPreferences("config", 0x0000);
		userId = sp.getString("userid", "");
		RelativeLayout backpress = (RelativeLayout) findViewById(R.id.backpress);
		backpress.setOnClickListener(this);
		TextView title_txt_center = (TextView) findViewById(R.id.title_txt_center);
		title_txt_center.setText("修改密码");

		comfirm_pay_btn = (Button) findViewById(R.id.comfirm_pay_btn);
		comfirm_pay_btn.setOnClickListener(this);
		phone = (EditText) findViewById(R.id.phone);
		code = (EditText) findViewById(R.id.code);
		 codeBtn = (Button) findViewById(R.id.codeBtn);
		codeBtn.setOnClickListener(this);
		gridPasswordView = (GridPasswordView) findViewById(R.id.pswView);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.backpress:
			finish();
			break;
		case R.id.codeBtn:
			String phoneStr = phone.getText().toString();
			if (TextUtils.isEmpty(phoneStr)) {
				Toast.makeText(PayPwdActivity.this, "手机号码不能为空", Toast.LENGTH_SHORT).show();
				return;
			}

			/** 去除手机号码中的空格 */
			if (phoneStr.contains(" ")) {
				phoneStr = phoneStr.replaceAll(" ", "");
			}

			/** 检查手机号码格式是否正确 */
			if (!EditUtils.isMobileNO(phoneStr)) {
				Toast.makeText(PayPwdActivity.this, "手机号码格式不正确", Toast.LENGTH_SHORT).show();
				return;
			}
			ConnectionDetector cd = new ConnectionDetector(PayPwdActivity.this);
			if (cd.isConnectingToInternet()) {
				String url = Config.UPDATE_PAY_SEND_CODE + "&phone=" + phoneStr
						+ "&userid="+userId;
				HttpUtils.doPostAsyn(url, "",mHandler, Config.CODE_UPDATE_PAY_SEND_PWD);
			}
			break;
		case R.id.comfirm_pay_btn:
			String new_pwdStr = gridPasswordView.getPassWord();
			String codeStr = code.getText().toString();
			if (TextUtils.isEmpty(codeStr)){
				Toast.makeText(PayPwdActivity.this, "验证码不能为空", Toast.LENGTH_SHORT).show();
				return;
			}
			if (TextUtils.isEmpty(new_pwdStr)){
				Toast.makeText(PayPwdActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
				return;
			}
			String url = Config.UPDATE_PAY_PWD + "&paypassword="
					+ new_pwdStr + "&phone=" + phone.getText().toString()+ "&u_ip="+ codeStr;
			HttpUtils.doPostAsyn(url, "",mHandler, Config.CODE_UPDATE_PAY_PWD);
			break;
		default:
			break;
		}
	}

	private Handler mHandler = new Handler() {

		public void handleMessage(Message msg) {
			switch (msg.what) {
			case Config.CODE_UPDATE_PAY_PWD:
				String reString = msg.obj.toString();
				try {
					JSONObject jsonObject = new JSONObject(reString);
					int error = jsonObject.getInt("error");
					if (error == 3) {
						startActivity(new Intent(PayPwdActivity.this,
								com.lzb.newloan.InvestPersonActivity.class));
						finish();
					}
					Toast.makeText(PayPwdActivity.this,
							jsonObject.getString("msg"), Toast.LENGTH_SHORT)
							.show();
				} catch (JSONException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}

				break;
			case Config.CODE_UPDATE_PAY_SEND_PWD:
					try {
						JSONObject jsonObject = new JSONObject(msg.obj.toString());
						int error = jsonObject.getInt("error");
						if (error == 0) {
							timeC = new TimeCount(180000, 1000, codeBtn, "重新获取");
							timeC.start();
							Toast.makeText(PayPwdActivity.this,
									"发送成功", Toast.LENGTH_SHORT)
									.show();
						}else {
							Toast.makeText(PayPwdActivity.this,
									jsonObject.getString("msg"), Toast.LENGTH_SHORT)
									.show();
						}
					} catch (JSONException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
					break;
			case Config.CODE_URL_ERROR:
				Toast.makeText(PayPwdActivity.this, "url错误",
						Toast.LENGTH_SHORT).show();
				break;
			case Config.CODE_NET_ERROR:
				Toast.makeText(PayPwdActivity.this, "网络错误",
						Toast.LENGTH_SHORT).show();
				break;

			default:
				break;
			}
		};
	};

	@Override
	public void processMessage(Message message) {
		// TODO Auto-generated method stub
		
	}

}
